<!--
	作者：offline
	时间：2018-08-15
	描述：公司信息
-->
<template>
	<div class="m-view-companyInfo m-window">
		<div class="m-condition">
			<el-row :gutter="10">
				<el-col :span="6">
					<el-form ref="ruleForm" label-width="90px" :inline-message="true" label-position="left">
						<el-form-item label="编号/名称" class="rightItem">
							<el-input v-model="name" placeholder="请输入公司编号/名称" clearable @keyup.enter.native="search"></el-input>
						</el-form-item>
					</el-form>
				</el-col>
				<el-col :span="2" :offset="8">
					<el-checkbox v-model="state" @change="changeState">显示停用</el-checkbox>
				</el-col>
				<el-col :span="2">
					<el-button type="primary" @click="search(1)">查询</el-button>
				</el-col>
				<el-col :span="2">
					<el-button type="primary" @click="add" :disabled="btnDisabled">引入</el-button>
				</el-col>
				<el-col :span="2">
					<el-button type="primary" @click="edit" :disabled="btnDisabled">编辑</el-button>
				</el-col>
				<!-- <el-col :span="2">
                    <el-button type="primary" @click="uploadFile" :disabled="btnDisabled">上传文件</el-button>
				</el-col>-->
				<el-col :span="2" v-if="delShow">
					<el-button type="primary" @click="del">停用</el-button>
				</el-col>
				<el-col :span="2" v-if="reuseShow">
					<el-button type="primary" @click="reuse">启用</el-button>
				</el-col>
			</el-row>
		</div>
		<div class="m-agTable m-bottom">
			<ag-grid-vue
				class="m-agTableContent ag-theme-balham"
				:gridOptions="gridOptions"
				:columnDefs="columnDefs"
				:rowData="rowData"
				v-loading="loading"
			></ag-grid-vue>
		</div>
		<div class="m-page">
			<el-pagination
				@size-change="handleSizeChange"
				@current-change="handleCurrentChange"
				@prev-click="handlePrevClick"
				@next-click="handleNextClick"
				:current-page="currentPage"
				:page-sizes="[30,50,100,150,200]"
				:page-size="pageSize"
				layout="total, sizes, prev, pager, next, jumper"
				:total="total"
			></el-pagination>
		</div>

		<!--弹框 -->
		<el-dialog
			:title="title"
			v-if="dialogVisible"
			:visible.sync="dialogVisible"
			width="50%"
			:before-close="handleClose"
			v-dialogDrag
			:close-on-click-modal="false"
			:close-on-press-escape="false"
			@opened="showCallback"
		>
			<el-form
				:model="ruleForm"
				ref="ruleForm"
				label-width="100px"
				class="demo-ruleForm"
				:rules="rules"
			>
				<el-form-item label="公司编号" placeholder="请输入公司编码" prop="companyCode">
					<el-input
						v-model="ruleForm.companyCode"
						placeholder="请输入编码"
						clearable
						@focus="showCompanyDialog"
						:disabled="inputDis"
					>
						<el-button slot="append" icon="el-icon-search" v-if="btnShow" @click="showCompanyDialog"></el-button>
					</el-input>
				</el-form-item>
				<el-form-item label="公司名称" placeholder="请输入公司名称" prop="companyName">
					<el-input v-model="ruleForm.companyName" placeholder="请输入名称" clearable :disabled="inputDis"></el-input>
				</el-form-item>
				<el-form-item label="地址" placeholder="请输入地址" prop="address">
					<el-input v-model="ruleForm.address" placeholder="请输入地址(200字)" maxlength="200"></el-input>
				</el-form-item>
				<el-form-item label="备注" placeholder="请输入备注" prop="remark">
					<el-input
						v-model="ruleForm.remark"
						placeholder="请输入备注信息(200字)"
						type="textarea"
						resize="none"
						maxlength="200"
					></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button @click="cancel" :disabled.sync="disState">取消</el-button>
				<el-button type="primary" @click="saveData" :disabled.sync="disState">保存</el-button>
				<el-button type="primary" @click="saveDataAgain" :disabled.sync="disState" v-if="btnShow">保存并继续</el-button>
			</span>
		</el-dialog>

		<el-dialog
			title="上传文件"
			v-if="upLoadFileDialogVisible"
			:visible.sync="upLoadFileDialogVisible"
			width="50%"
			:before-close="upLoadFileClose"
			v-dialogDrag
			:close-on-click-modal="false"
		>
			<uploadfile ref="uploadfile"></uploadfile>
		</el-dialog>

		<el-dialog
			title="SAP公司信息"
			v-if="companyDialogVisible"
			:visible.sync="companyDialogVisible"
			width="50%"
			:before-close="companyInfoClose"
			v-dialogDrag
			:close-on-click-modal="false"
			:close-on-press-escape="false"
		>
			<sapCompanyInfo
				ref="sapCompanyInfo"
				@setSapCompanyInfo="getSelectedSapCompanyInfo"
				:showIsSelected="isShowSelected"
			></sapCompanyInfo>
		</el-dialog>

		<el-dialog
			title="SRM公司信息"
			v-if="srmCompanyDialogVisible"
			:visible.sync="srmCompanyDialogVisible"
			width="50%"
			:before-close="srmCompanyInfoClose"
			v-dialogDrag
			:close-on-click-modal="false"
			:close-on-press-escape="false"
		>
			<srmCompanyInfo ref="srmCompanyInfo" :selection="selection"></srmCompanyInfo>
		</el-dialog>
	</div>
</template>

<script>
import _Api from "@/api";
// import excel from "@/views/excel";
import localeText from "@/i18n/agGridText";
import uploadfile from "@/components/upLoadFile.vue";
import sapCompanyInfo from "@/components/sapCompanyInfo.vue";
import srmCompanyInfo from "@/components/srmCompanyInfo.vue";
import filterComponents from "@/components/filterComponents.js"
import gridOptions from 'lesso-common/public/gridOptions';

export default {
	data () {
		var checkVal = (rule, value, callback) => {
			setTimeout(() => {
				if (value) {
					if (!value.trim()) {
						callback(new Error("不能为空！"));
					}
					else
						callback();
				} else {
					callback(new Error("不能为空！"));
				}
			}, 100);
		};
		return {
			textChange: 0,
			name: "",
			columnDefs: [],
			rowData: [],
			currentPage: 1,
			pageSize: 30,
			total: 0,
			loading: false,
			state: false,
			btnDisabled: false,
			delShow: true,
			reuseShow: false,
			title: "",
			dialogVisible: false,
			ruleForm: {},
			btnShow: true,
			inputDis: true,
			disState: false,
			rules: {
				companyCode: [{
					required: true,
					//						message:"请选择公司",
					//						trigger:"change"
					validator: checkVal,
					trigger: "change"
				}],
				companyName: [{
					required: true,
					//						message:"公司名称不能为空",
					//						trigger:"change"
					validator: checkVal,
					trigger: "change"
				}],
				address: [{
					min: 0,
					max: 200,
					trigger: "change",
					message: "长度在200个字符"
				}],
				remark: [{
					min: 0,
					max: 200,
					triggerr: "change",
					message: "长度在200个字符"
				}]
			},
			companyDialogVisible: false,
			keyWord: "",
			selCompanyDefs: [],
			selCompanyData: [],
			mode: "",
			upLoadFileDialogVisible: false,
			srmCompanyDialogVisible: false,
			selection: "",
			isShowSelected: true
		}
	},
	props: ['sapCompanyInfo'],
	created () {

	},
	mounted () {
		this.search(1);
	},
	beforeMount () {
		this.gridOptions = Object.assign({
			context: {
				componentParent: this
			},
			rowSelection: "multiple",
			enableColResize: true,
			localeText: localeText,
			suppressRowClickSelection: true,
			// getContextMenuItems:this.getContextMenuItems,
			onGridReady: function (params) {
				params.api.sizeColumnsToFit();
			}
			//      	    alwaysShowStatusBar:true,
			//      	    enableStatusBar:true,
			//      	    enableRangeSelection:true
		}, gridOptions())
		this.columnDefs = [
			{ headerName: "公司编号", field: "companyCode", checkboxSelection: true, headerCheckboxSelection: true, headerCheckboxSelectionFilteredOnly: true },
			{ headerName: "公司名称", field: "companyName" },
			{ headerName: "地址", field: "address" },
			{ headerName: "备注", field: "remark", suppressSizeToFit: true },
			{				headerName: "状态", field: "state",
				cellRenderer: function (params) {
					if (params.value == "1")
						return '<span class="ag-icon">停用</span>';
					else if (params.value == "0")
						return '<span class="ag-icon">启用</span>';
				}
			}];
		this.selCompanyOptions = {
			context: {
				componentParent: this
			},
			rowSelection: "single",
			enableColResize: true,
			localeText: localeText
		};
		this.selCompanyDefs = [
			{ headerName: "公司名称", field: "" },
			{ headerName: "公司编号", field: "" }];
	},
	methods: {
		getContextMenuItems () {
			return [];
		},
		uploadFile () {
			this.upLoadFileDialogVisible = true;
		},
		showCallback () {
			this.$refs.ruleForm.clearValidate()
		},
		edit () {
			var row = this.gridOptions.api.getSelectedRows();
			if (row.length <= 0) {
				this.$alert("提示：请选择需要编辑的数据", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				return;
			}
			if (row.length > 1) {
				this.$alert("提示：只能选择单条数据进行编辑操作！", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				return;
			}
			this.title = "公司编辑";
			this.btnShow = false;
			this.ruleForm = row[0];
			this.mode = "edit";
			this.dialogVisible = true;
		},
		search (flag) {
			var s;
			if (this.state == false)
				s = 0;
			else
				s = 1
			this.name = this.name.replace(/\s+/g, "");
			var obj = {
				"state": s,
				"name": this.name
				//					"current":this.currentPage,
				//					"size":this.pageSize
			};
			if (flag === 1)
				this.currentPage = 1;
			obj.current = this.currentPage;
			obj.size = this.pageSize;
			var that = this;
			this.loading = true;

			_Api.getCompanyInfo(obj).then(function (response) {
				that.textChange = 0;
				var data = response.data;
				if (data.code == "1") {
					if (data.total > 0) {
						that.gridOptions.api.setRowData(data.rows.splice(0));
						that.total = data.total;
						that.gridOptions.api.sizeColumnsToFit();
					}
					else {
						that.gridOptions.api.setRowData([]);
						that.total = 0;
					}
				} else {
					that.$alert(data.info, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					});
				}
				that.loading = false;
			}).catch((err) => {
				that.textChange = 0;
				that.$alert("操作失败", {
					showClose: false,
					confirmButton: "确定",
					customClass: "alertFailure"
				});
				that.loading = false;
			})
		},
		handleSizeChange (params) {
			this.currentPage = 1;
			this.pageSize = params;
			this.search();
		},
		handleCurrentChange (params) {
			if (this.textChange === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			this.search();
		},
		handlePrevClick (params) {
			if (this.textChange === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			// this.search();
		},
		handleNextClick (params) {
			if (this.textChange === 1)
				this.currentPage = 1;
			else
				this.currentPage = params;
			// this.search();
		},
		changeState (params) {
			if (params == true) {
				this.delShow = false;
				this.reuseShow = true;
				this.btnDisabled = true;
			} else {
				this.delShow = true;
				this.reuseShow = false;
				this.btnDisabled = false;
			}
			this.search();
		},
		del () {
			var row = this.gridOptions.api.getSelectedRows();
			if (row.length <= 0) {
				this.$alert("提示：请选择需要停用的数据", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				return;
			}
			var that = this;
			this.$confirm("是否确认停用该数据？", "消息提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				distinguishCancelAndClose: true
			}).then(() => {
				var id = '';
				for (var i = 0; i < row.length; i++) {
					id += row[i].id + ',';
				}
				id = id.substr(0, id.length - 1);
				var obj = {
					id: id,
					state: 1,
					modifyer: this.$global.user.loginName,
					modifyerId: this.$global.user.id
				}
				_Api.delCompanyInfo(obj).then(function (response) {
					var data = response.data;
					if (data.code == "1") {
						that.$message({
							message: "操作成功",
							type: "success"
						});
					} else {
						that.$alert("操作失败！", {
							showClose: false,
							confirmButton: "确定",
							customClass: "alertFailure"
						});
					}
					that.search();
				}).catch((err) => {
					that.$alert(err.data.message, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					});
				})
			}).catch(() => {

			})
		},
		add () {
			this.ruleForm = {};
			this.title = "公司引入";
			this.btnShow = true;
			this.mode = "add";
			this.dialogVisible = true;
		},
		cancel () {
			this.$refs.ruleForm.resetFields();
			this.dialogVisible = false;
			this.search();
		},
		handleClose (done) {
			this.$refs.ruleForm.resetFields();
			this.$refs.ruleForm.clearValidate();
			done();
			this.search();
		},
		saveData () {
			var that = this;
			if (this.mode == "add") {
				this.$refs["ruleForm"].validate((valid) => {
					if (!valid) {
						return false;
					}
					that.disState = true;
					that.ruleForm.creator = this.$global.user.loginName;
					that.ruleForm.creatorId = this.$global.user.id;
					_Api.addCompanyInfo(that.ruleForm).then(function (response) {
						var data = response.data;
						if (data.code == "1") {
							that.$message({
								message: "保存成功",
								type: "success"
							});
							that.ruleForm = {};
							that.dialogVisible = false;
							that.search();
						} else {
							that.$alert(data.info, {
								showClose: false,
								confirmButton: "确定",
								customClass: "alertFailure"
							});
						}
						that.disState = false;
					}).catch((err) => {
						that.$alert(err.data.message, {
							showClose: false,
							confirmButton: "确定",
							customClass: "alertFailure"
						});
						that.disState = false;
					})
				});
			} else if (this.mode == "edit") {
				this.$refs["ruleForm"].validate((valid) => {
					if (valid) {
						that.disState = true;
						that.ruleForm.modifyer = this.$global.user.loginName;
						that.ruleForm.modifyerId = this.$global.user.id;
						_Api.editCompanyInfo(that.ruleForm).then(function (response) {
							var data = response.data;
							if (data.code == "1") {
								that.$message({
									message: "保存成功",
									type: "success"
								});
								that.ruleForm = {};
								that.dialogVisible = false;
								that.search();
							} else {
								that.$alert(data.info, {
									showClose: false,
									confirmButton: "确定",
									customClass: "alertFailure"
								});
							}
							that.disState = false;
						}).catch((err) => {
							that.$alert(err.data.message, {
								showClose: false,
								confirmButton: "确定",
								customClass: "alertFailure"
							});
							that.disState = false;
						})
					} else {
						return false;
					}
				});
			}

		},
		saveDataAgain () {
			var that = this;
			this.$refs["ruleForm"].validate((valid) => {
				if (valid) {
					that.disState = true;
					that.ruleForm.creator = this.$global.user.loginName;
					that.ruleForm.creatorId = this.$global.user.id;
					_Api.addCompanyInfo(that.ruleForm).then(function (response) {
						var data = response.data;
						if (data.code == "1") {
							that.$message({
								message: "保存成功",
								type: "success"
							});
							that.ruleForm = {};
							that.search();
						} else {
							that.$alert(data.info, {
								showClose: false,
								confirmButton: "确定",
								customClass: "alertFailure"
							});
						}
						that.disState = false;
					}).catch((err) => {
						that.$alert(err.data.message, {
							showClose: false,
							confirmButton: "确定",
							customClass: "alertFailure"
						});
						that.disState = false;
					})
				} else {
					return false;
				}
			});
		},
		showCompanyDialog () {
			if (this.mode == "edit") return;
			this.companyDialogVisible = true;
			this.$nextTick(() => {
				this.$refs.sapCompanyInfo.getSapCompany();
			})
			// this.$refs.sapCompanyInfo.getSapCompany();
			//this.srmCompanyDialogVisible = true;
			//this.selection = "multiple"
		},
		selectedCompany () {
			var row = this.selCompanyOptions.api.getSelectedRows();
			if (row.length <= 0) {
				this.$alert("提示：请选择需要的数据", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				return;
			}
			this.companyDialogVisible = false;
			//				this.ruleForm.companyCode = this.row[0].
		},
		onFilterChanged () {
			var that = this;
			setTimeout(function () {
				that.selCompanyOptions.api.setQuickFilter(that.keyWord);
			}, 200);
		},
		reuse () {
			var row = this.gridOptions.api.getSelectedRows();
			if (row.length <= 0) {
				this.$alert("提示：请选择需要启用的数据", {
					showClose: false,
					confirmButtonText: "确定",
					customClass: "alertPrompt"
				});
				return;
			}
			var that = this;
			this.$confirm("是否确认启用该数据？", "消息提示", {
				confirmButtonText: "确定",
				cancelButtonText: "取消",
				distinguishCancelAndClose: true
			}).then(() => {
				var id = '';
				for (var i = 0; i < row.length; i++) {
					id += row[i].id + ',';
				}
				id = id.substr(0, id.length - 1);
				var obj = {
					id: id,
					state: 0,
					modifyer: this.$global.user.loginName,
					modifyerId: this.$global.user.id
				}
				_Api.delCompanyInfo(obj).then(function (response) {
					var data = response.data;
					if (data.code == "1") {
						that.$message({
							message: "操作成功",
							type: "success"
						});
					} else {
						that.$alert("操作失败！", {
							showClose: false,
							confirmButton: "确定",
							customClass: "alertFailure"
						});
					}
					that.search();
				}).catch((err) => {
					that.$alert(err.data.message, {
						showClose: false,
						confirmButton: "确定",
						customClass: "alertFailure"
					});
				})
			}).catch(() => {

			})
		},
		upLoadFileClose (done) {
			done();
		},
		companyInfoClose (done) {
			done();
		},
		getSelectedSapCompanyInfo (msg) {
			if (msg[0]) {
				this.companyDialogVisible = false;
				this.$set(this.ruleForm, 'companyCode', msg[0].companyCode);
				this.$set(this.ruleForm, 'companyName', msg[0].companyName);
				this.$set(this.ruleForm, 'address', msg[0].address);
			}
		},
		srmCompanyInfoClose (done) {
			done();
		}
	},
	components: {
		uploadfile,
		sapCompanyInfo,
		srmCompanyInfo,
		filterComponents
	},
	watch: {
		name (value) {
			if (value)
				this.textChange = 1;
			else
				this.textChange = 0;
		}
	}
}
</script>